﻿#region Namespace References

using System;
using DynConDevCorp.OpenArch.SmartData.Interfaces.EventArgClasses;
using DynConDevCorp.OpenArch.SmartData.Samples.SimpleInvoicing;

#endregion

namespace DynConDevCorp.OpenArch.SmartData.Samples.SimpleInvoicing
{
    internal static class Program
    {
        #region Private Static Methods

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        private static void Main()
        {
            Invoice invoice = new Invoice();
            invoice.InvoiceNumber.Value = "1000";
            invoice.TaxRate.Value = 0.07M;
            invoice.ShippingRate.Value = 1.23M;
            invoice.FieldValueChanged += Invoice_FieldChanged;

            invoice.Items.CollectionChanged += Items_CollectionChanged;

            InvoiceLineItem invoiceLineItem = new InvoiceLineItem();
            Console.WriteLine("Creating Item:  Qty = 10, UnitPrice = $19.95 Weight = 0.75 Lbs");
            invoiceLineItem.Quantity.Value = 10;
            invoiceLineItem.UnitPrice.Value = 19.95M;
            invoiceLineItem.Weight.Value = 0.75M;
            invoice.AddItem(invoiceLineItem);

            Console.WriteLine("Invoice Totals Calculated As");
            Console.WriteLine("\tInvoice Items Total: {0}", invoice.ItemsTotalPrice);
            Console.WriteLine("\tInvoice TAX:         {0}", invoice.ItemsTotalTax);
            Console.WriteLine("\tInvoice Weight:      {0}", invoice.ItemsTotalWeight);
            Console.WriteLine("\tInvoice Shipping:    {0}", invoice.Shipping);
            Console.WriteLine("\tInvoice GRAND TOTAL: {0}", invoice.InvoiceTotal);

            invoiceLineItem.FieldValueChanged += InvoiceLineItem_FieldChanged;
            Console.WriteLine();
            Console.WriteLine("Testing Event Propogation");
            Console.WriteLine("\t#{0} Quantity:{1}  Unit Price {2} Extended Price: {3}", invoiceLineItem.LineNumber, invoiceLineItem.Quantity, invoiceLineItem.UnitPrice, invoiceLineItem.ExtendedPrice);
            Console.WriteLine("Changing Quantity to 15....");
            invoiceLineItem.Quantity.Value = 15;
            Console.WriteLine("\t#{0} Quantity:{1}  Unit Price {2} Extended Price: {3}", invoiceLineItem.LineNumber, invoiceLineItem.Quantity, invoiceLineItem.UnitPrice, invoiceLineItem.ExtendedPrice);
            Console.WriteLine("Changing Unit Price to $14.95....");
            invoiceLineItem.UnitPrice.Value = 14.95M;
            Console.WriteLine("\t#{0} Quantity:{1}  Unit Price {2} Extended Price: {3}", invoiceLineItem.LineNumber, invoiceLineItem.Quantity, invoiceLineItem.UnitPrice, invoiceLineItem.ExtendedPrice);
            Console.WriteLine();
            Console.WriteLine("Demonstration Complete!!!!!");
            Console.WriteLine();

        }

        static void Items_CollectionChanged(object sender, CollectionChangedEventArgs e)
        {
            InvoiceLineItem invoiceLineItem = e.Item as InvoiceLineItem;
            Console.WriteLine("Invoice Items - Change {0} Item Line #:{1}", e.ChangeType,
                              invoiceLineItem != null ? invoiceLineItem.LineNumber.Value.ToString() : "{N/A}");
        }

        static void InvoiceLineItem_Changed(object sender, ValueChangedEventArgs<object> e)
        {
            InvoiceLineItem invoiceLineItem = sender as InvoiceLineItem;
            Console.WriteLine("Invoice Line:{0} - Changed from {1} to {2}", invoiceLineItem.LineNumber.Value, e.OldValue, e.NewValue);
        }

        static void InvoiceLineItem_FieldChanged(object sender, FieldValueChangedEventArgs e)
        {
            InvoiceLineItem invoiceLineItem = sender as InvoiceLineItem;
            Console.WriteLine("Invoice Line:{0} - {1} Changed from {2} to {3}", invoiceLineItem.LineNumber.Value, e.FieldName, e.OldValue, e.NewValue);
        }
        static void Invoice_FieldChanged(object sender, FieldValueChangedEventArgs e)
        {
            Invoice invoice = sender as Invoice;
            Console.WriteLine("Invoice {0} - {1} Changed from {2} to {3}", invoice.InvoiceNumber.Value, e.FieldName, e.OldValue, e.NewValue);
        }

        #endregion
    }
}